<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/3/21
 * Time: 14:45
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\admin\controller\weixin;

use app\common\controller\AdminController;
use app\common\model\WeixinExpressConfig;
use app\common\model\WeixinExpressOrder;
use app\common\model\WeixinExpressOrderList;
use EasyAdmin\annotation\ControllerAnnotation;
use app\common\lib\ArrClass;
use app\common\lib\Show;
use app\common\lib\Status;
use think\App;
use think\Exception;

/**
 * Class Config
 * @package app\admin\controller\weixin
 * @ControllerAnnotation(title="快递订单")
 */
class ExpressOrder extends AdminController
{
    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\common\model\WeixinExpressOrder();

        $this->assign('getStatusList', $this->model->getStatusList());
        $this->with = ["wechat",'shop'];
    }

    /**
     * 列表
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where,$map) = $this->buildTableParames(['shop.title'],'o');
            if(!empty($map)){
                if (!empty($map['shop.title'])) {
                    $where[] = [
                        ['s.title','like',"%".$map['shop.title']."%"]
                    ];
                }
            }
            $where[] = [
                ['o.pay_status','=',1]
            ];
            $list = $this->model
                ->alias('o')
                ->join('mall_shop s','s.id = o.shop_id')
                ->field("o.*")
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->with($this->with)
                ->paginate($limit)
                ->toArray();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $list['total'],
                'data'  => $list['data'],
            ];
            return json($data);
        }
        return $this->fetch();
    }
    /**
     * @NodeAnotation (title="查看详情")
     * @param $id
     * @return mixed
     * @throws Exception
     */
    public function edit($id)
    {
        $row = $this->model->getBaseById($id, '*', 'wechat');
        if (empty($row)) {
            $this->error("信息不存在");
        }
        //查询所有订单
        $list = (new WeixinExpressOrderList())->getBaseByList(["order_id" => $row['id']]);
        $row['list'] = $list;

        return $this->fetch('', [
            "row" => $row
        ]);
    }

    public function saveStatus()
    {
        $id = $this->request->param('id', 0, 'intval');
        $type = $this->request->param('type', 1, 'intval');
        if ($id == 0) {
            return Show::json_show(Status::status_error, "发生错误");
        }

        $obj_order_list = new WeixinExpressOrderList();
        $obj_order = new WeixinExpressOrder();
        $obj_order->startTrans();
        try {
            if ($type == 1) {
                $row = $obj_order_list->getBaseById($id);
                if (empty($row)) {
                    return Show::json_show(Status::status_error, '信息不存在');
                }
                $obj_order_list->updateDataSave($id, [
                    "status" => 1,
                    "update_time" => time()
                ]);
                $arr = $obj_order_list->getBaseByList(['status' => 0, 'order_id' => $row['order_id']]);
                if (empty($arr)) {
                    $obj_order->updateDataSave($row['order_id'], [
                        "status" => 1
                    ]);
                }
            } else {
                $obj_order->updateDataSave($id, [
                    "status" => 1,
                    "update_time" => time()
                ]);
                $obj_order_list->where('order_id', $id)
                    ->update([
                        "status" => 1,
                        "update_time" => time()
                    ]);
            }
            $obj_order->commit();
        } catch (\Exception $e) {

            $obj_order->rollback();
            return Show::json_show(Status::status_error, "操作失败");
        }
        return Show::json_show(Status::status_normal, "操作成功");
    }

}